<!DOCTYPE html>
<html>
	<head>
		<title>设置个人资料</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
		<script src="/js/jquery/jquery.min.js"></script>
		<script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/myInfo.css" />
		<script src="/js/plugins/jrender/jrender.min.js"></script>
		<link rel="stylesheet" href="/js/plugins/dialog2/dialog.css" />
		<script src="/js/plugins/dialog2/dialog.min.js"></script>
		<script src="/js/plugins/form/jquery.form.js"></script>
			
	</head>

	<body>
		<form id="fileForm" action="/images" method="post" enctype="multipart/form-data">
			<input name="file" type="file" id="fileInput" style="display: none">
		</form>
		<div class="search-head">
			<div class="row nav-search">
				<div class="col">
					<a href="javascript:window.history.go(-1);">
						<span>取消</span>
					</a>
				</div>
				<div class="col">
					<span>设置个人资料</span>
				</div>
				<div class="col" >
					<a href="#" id="saveBtn" >
						<span>保存</span>
					</a>
				</div>
			</div>
		</div>

		<div class="container info">
			<div class="myBg">
				<img id="headImg" render-src="headImgUrl" class="rounded-circle"/>
				<div class="editHead" id="headBtn" ><i class="fa fa-pencil-square fa-lg " aria-hidden="true"></i></div>
				<button  class="btn btn-outline-light ibtn " id="coverBtn">设置背景</button>
			</div>

			<form id="infoForm" method="post" >
					<!--表单做put提交 更新操作-->
					<input type="hidden" name="_method" value="put"/>

					<!--封面和头像的隐藏域-->
					<input type="hidden" name="headImgUrl" render-value="headImgUrl">
					<input type="hidden" name="coverImgUrl" render-value="coverImgUrl">
					<!--用来回显,不做修改-->
					<input type="hidden" name="email" render-value="email">
				
					<label>昵称</label><input type="text" name="nickName" render-value="nickName" class="form-control" >
					<label>性别</label><select class="form-control" name="gender" render-value="gender">
							<option value="-1">保密</option>
							<option value="1">男</option>
							<option value="0">女</option>
						</select>
					<label>地区</label><input type="text" name="place" render-value="place" class="form-control">
					<label>签名</label><textarea name="sign" class="form-control" rows="5" render-value="sign"></textarea>
			</form>
		</div>
		
<script>
	$(function () {
	    //定义flag true表示当前是头像上传事件 false 为背景上传
	    var flag = true;

	    var userJson = JSON.parse(sessionStorage.getItem("user"));
		$(".info").renderValues(userJson);
		//背景图片的回显
		$(".myBg").css('background-image','url('+userJson.coverImgUrl+')');
		//设置form表单的action地址
		$("#infoForm").attr("action","/users/"+userJson.id);

		$("#headBtn").click(function () {
			//头像按钮被点击 修改flag为true 判断flag 来确认现操作哪个按钮被点击了
		    flag = true;
		    //触发文件上传框的点击事件
			$("#fileInput").click();
        });
		//文本框使用change事件
        $("#fileInput").change(function () {
            //判断是否有图片
            if ($(this).val()) {
                $("#fileForm").ajaxSubmit(function (data) {
                    if (data.status == 1) {
						//判断是修改头像还是修改背景
                        if (flag) {
                            //回显修改后的头像
							$("#headImg").attr("src",data.url);
							//将修改后的头像 同时反映到name="headImgUrl",当进行奥村操作的时候,将新的头像url提交到后台
							$("[name='headImgUrl']").val(data.url);
                        }else{
                            //回显更换的背景样式
                            $(".myBg").css("background-image",'url('+data.url+')');
                            //将更换后的背景的 url给name='coverImgUrl 保存时提交使用
                            $("[name='coverImgUrl']").val(data.url);
						}
                    }
                });
            }
        });
        //设置背景按钮的点击事件
		$("#coverBtn").click(function () {
            //背景按钮被点击 则将flag改为false 表示此时文本上传做的是背景上传
            flag = false;
		    //调用文件上传文本框的点击事件
            $("#fileInput").click();
        });
		$("#saveBtn").click(function () {
			$("#infoForm").ajaxSubmit(function (data) {
                //更新成功时,将存储在sessionStorage的用户也更新
                var user = JSON.stringify(data.result);
                sessionStorage.setItem("user", user);
                //保存成功跳转个人资料页面
                window.location.href = "/mine/profiles.html";
            });
        });
    });
</script>
		
	</body>
</html>
